package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.base.domain.vo.SyYjVo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_sj.domain.ReportSjkyqdBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjkyqdBaDetailMapper;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjKyqdBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjKyqdBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjKyqdBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjKyqdBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjKyqdBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆抗压强度试验检测记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-29
 */
@RequiredArgsConstructor
@Service
public class ReportSjKyqdBsInfoServiceImpl implements IReportSjKyqdBsInfoService {

    private final ReportSjKyqdBsInfoMapper baseMapper;
    private final ReportSjkyqdBaDetailMapper detailMapper;

    /**
     * 查询砂浆抗压强度试验检测记录
     */
    @Override
    @FIllBasicInfo
    public ReportSjKyqdBsInfoVo queryById(Long excelId) {
        ReportSjKyqdBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSjkyqdBaDetail> detailList = detailMapper.selectListByExcelId(excelId);
        if (CollUtil.isNotEmpty(detailList)) {
            vo.setDetailList(detailList);
        }
        return vo;
    }

    /**
     * 查询砂浆抗压强度试验检测记录列表
     */
    @Override
    public TableDataInfo<ReportSjKyqdBsInfoVo> queryPageList(ReportSjKyqdBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjKyqdBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjKyqdBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆抗压强度试验检测记录列表
     */
    @Override
    public List<ReportSjKyqdBsInfoVo> queryList(ReportSjKyqdBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjKyqdBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjKyqdBsInfo> buildQueryWrapper(ReportSjKyqdBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjKyqdBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjKyqdBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjKyqdBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSjKyqdBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSjKyqdBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSjKyqdBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSjKyqdBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSjKyqdBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSjKyqdBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSjKyqdBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSjKyqdBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSjKyqdBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSjKyqdBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSjKyqdBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSjKyqdBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSjKyqdBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSjKyqdBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSjKyqdBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSjKyqdBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSjKyqdBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSjKyqdBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSjKyqdBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSjKyqdBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSjKyqdBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSjKyqdBsInfo::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghfs()), ReportSjKyqdBsInfo::getYanghfs, bo.getYanghfs());
        return lqw;
    }

    /**
     * 新增砂浆抗压强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(ReportSjKyqdBsInfoBo bo) {
        ReportSjKyqdBsInfo add = BeanUtil.toBean(bo, ReportSjKyqdBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        //插入子表 空数据
        List<ReportSjkyqdBaDetail> detailList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            ReportSjkyqdBaDetail detail = new ReportSjkyqdBaDetail();
            detail.setExcelId(bo.getExcelId());
            detailList.add(detail);
        }
        detailMapper.insertBatch(detailList);
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆抗压强度试验检测记录
     */
    @Override
    @DSTransactional
    public Boolean updateByBo(ReportSjKyqdBsInfoBo bo) {
        ReportSjKyqdBsInfo update = BeanUtil.toBean(bo, ReportSjKyqdBsInfo.class);
        validEntityBeforeSave(update);
        //更新子表
        List<ReportSjkyqdBaDetail> detailList = bo.getDetailList();
        if (detailList != null && detailList.size() != 0) {
            detailMapper.updateBatchById(detailList);
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjKyqdBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆抗压强度试验检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SyYjVo> getQddj(String qddj) {
        return baseMapper.getQddj(qddj);
    }
}
